package com.inspur.gs.sbssdl.gzgl.ssqyj.ssqyj.back.bizactions;

import com.inspur.edp.bef.api.lcp.IStandardLcp;
import com.inspur.edp.bef.core.LcpUtil;
import com.inspur.edp.bef.engine.entity.AssociationInfo;
import com.inspur.edp.cef.api.message.*;
import com.inspur.edp.bef.api.action.determination.*;
import com.inspur.edp.bef.api.action.validation.*;
import com.inspur.edp.cef.entity.changeset.*;
import com.inspur.edp.bef.api.be.*;
import com.inspur.edp.bef.api.action.VoidActionResult;
import com.inspur.edp.bef.spi.action.AbstractManagerAction;
import com.inspur.edp.cef.entity.entity.IEntityData;
import com.inspur.edp.cef.entity.entity.IEntityDataCollection;
import com.inspur.edp.cef.entity.entity.IValuesContainer;

import java.util.ArrayList;
import java.util.List;

public class gzssqyjsubmitslMgrAction extends AbstractManagerAction<VoidActionResult> {
    private String id;

    public gzssqyjsubmitslMgrAction(IBEManagerContext managerContext, String id) {
        super(managerContext);
        this.id = id;
    }

    // 所属权
    private String ssqyjCfg = "com.inspur.gs.sbssdl.gzgl.ssqyj.ssqyj.back.gzssqyj";

    // 工装台账
    private String gztzCfg = "com.inspur.gs.sbssdl.gzgl.ledger.ledger.back.FrockLedger";

    /**
     * 提交锁定
     * 1.增加工装记录
     * 2.提交履历记录
     * 3.新增履历记录
     */
    @Override
    public void execute() {
        IStandardLcp ssqLcp = LcpUtil.getLcp(ssqyjCfg);
        IStandardLcp gztzLcp = LcpUtil.getLcp(gztzCfg);

        IEntityData ssqData = ssqLcp.retrieve(this.id).getData();
        IEntityDataCollection ssqMx = ssqData.getChilds().get("yjmx");

        for (IEntityData mx : ssqMx) {
            submitKc(ssqData, mx, gztzLcp);
        }
    }

    private IBusinessEntity getEntity(String dataId) {
        return (IBusinessEntity) super.getBEManagerContext().getEntity(dataId);
    }

    private IBEService getMgr() {
        return (IBEService) super.getBEManagerContext().getBEManager();
    }

    private void submitKc(IEntityData ssqData, IEntityData mx, IStandardLcp gzLcp) {
        String mxId = mx.getID();
        String yjdh = ssqData.getValue("sqdh").toString();

        IValuesContainer gztz = (IValuesContainer) mx.getValue("gzid");
        if (gztz == null || gztz.getValue("gzid") == null) {
            return;
        }
        String gzid = gztz.getValue("gzid").toString();
        IEntityData gzData = gzLcp.retrieve(gzid).getData();

        int zmsl = Integer.parseInt(gzData.getValue("zmsl").toString());
        int sdsl = Integer.parseInt(gzData.getValue("sdsl").toString());
        int mxSl = Integer.parseInt(mx.getValue("sl").toString());

        // 检查扣减日志是否存在,并且未通过
        IEntityDataCollection yjjls = gzData.getChilds().get("yjjl");
        boolean hasItem = yjjls.stream().anyMatch(item -> item.getValue("yjmxId") != null && item.getValue("yjmxId").toString().equals(mxId) && item.getValue("spzt").toString().equals("1"));
        if (hasItem) return;

        ModifyChangeDetail kcMcd = new ModifyChangeDetail(gzid);
        kcMcd.putItem("zmsl", zmsl - mxSl);
        kcMcd.putItem("sdsl", sdsl - mxSl);


        yjjls.stream().filter(item -> item.getValue("yjmxId") != null && item.getValue("yjmxId").toString().equals(mxId))
                .forEach(i -> {
                    ModifyChangeDetail mcd = new ModifyChangeDetail(i.getID());
                    mcd.putItem("spzt", "1");
                    kcMcd.addChildChangeSet("yjjl", mcd);
                });
        gzLcp.modify(kcMcd);

        // 新增库存单
        IEntityData gzDataNew = gzLcp.retrieveDefault();
        ModifyChangeDetail newMcd = copyIEntityData(gzData, gzDataNew);

        // 修改数量
        newMcd.putItem("zmsl", mxSl);
        newMcd.putItem("sdsl", 0);
        newMcd.putItem("pcsl", 0);

        // 修改责任人
        IValuesContainer zrr = (IValuesContainer) mx.getValue("zrr");
        AssociationInfo zrrInfo = null;
        if (zrr != null && zrr.getValue("zrr") != null) {
            String zrrId = zrr.getValue("zrr").toString();
            if (!zrrId.isEmpty()) {
                zrrInfo = new AssociationInfo();
                zrrInfo.setValue("zrr", zrrId);
            }
        }
        newMcd.putItem("zrr", zrrInfo);

        // 修改部门
        IValuesContainer yjbm = (IValuesContainer) ssqData.getValue("yjbm");
        if (yjbm != null && yjbm.getValue("yjbm") != null) {
            String yjdmId = yjbm.getValue("yjbm").toString();
            if (!yjdmId.isEmpty()) {
                AssociationInfo bmidInfo = new AssociationInfo();
                bmidInfo.setValue("bmid", yjdmId);
                newMcd.putItem("bmid", bmidInfo);
            }
        }


        // 生成流转记录
        ArrayList<String> nodeCodes = new ArrayList<>();
        nodeCodes.add("yjjl");
        ArrayList<String> hierarchyIds = new ArrayList<>();
        hierarchyIds.add(gzDataNew.getID());
        IEntityData gcChild = gzLcp.retrieveDefaultChild(nodeCodes, hierarchyIds);

        ModifyChangeDetail mxDetail = new ModifyChangeDetail(gcChild.getID());
        mxDetail.putItem("spzt", "1");//待审核
        mxDetail.putItem("yjdh", yjdh);//单号
        mxDetail.putItem("yjid", ssqData.getID());
        mxDetail.putItem("yjmxId", mxId);

        newMcd.addChildChangeSet("yjjl", mxDetail);

        gzLcp.modify(newMcd);

        gzLcp.save();
    }

    private ModifyChangeDetail copyIEntityData(IEntityData data, IEntityData copyData) {
        ModifyChangeDetail detail = new ModifyChangeDetail(copyData.getID());
        List<String> dataPropertyNames = data.getPropertyNames();
        for (String s : dataPropertyNames) {
            if (!s.equals("ID")) {
                if (data.getValue(s) != null) {
                    if (data.getValue(s) instanceof IValuesContainer) {
                        AssociationInfo info = new AssociationInfo();
                        Object value;
                        try {
                            value = ((IValuesContainer) data.getValue(s)).getValue(s);
                        } catch (RuntimeException e) {
                            value = null;
                        }
                        if (value != null) {
                            info.setValue(s, value);
                            detail.getPropertyChanges().put(s, info);
                        }
                    } else {
                        detail.putItem(s, data.getValue(s));
                    }
                }
            }
        }
        return detail;
    }
}